package com.xjrsoft.module.fabricapply.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.fabricapply.dto.AddFabricApplyDto;
import com.xjrsoft.module.fabricapply.dto.FabricApplyPageDto;
import com.xjrsoft.module.fabricapply.dto.UpdateFabricApplyDto;
import com.xjrsoft.module.fabricapply.entity.FabricApply;
import com.xjrsoft.module.fabricapply.service.IFabricApplyService;
import com.xjrsoft.module.fabricapply.vo.FabricApplyPageVo;
import com.xjrsoft.module.fabricapply.vo.FabricApplyVo;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.organization.entity.UserRoleRelation;
import com.xjrsoft.module.organization.service.IUserRoleRelationService;
import com.xjrsoft.module.organization.service.IUserService;
import com.xjrsoft.module.system.enums.RoleTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @title: 证照申请
 * @Author 管理员
 * @Date: 2024-12-17
 * @Version 1.0
 */
@RestController
@RequestMapping("/fabric" + "/fabricapply")
@Api(value = "/fabric" + "/fabricapply", tags = "证照申请代码")
@AllArgsConstructor
public class FabricApplyController {


    private final IFabricApplyService fabricApplyService;

    private final IUserService userService;

    @Resource
    private IUserRoleRelationService userRoleRelationService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "FabricApply列表(分页)")
    @SaCheckPermission("fabricapply:detail")
    public R page(@Valid FabricApplyPageDto dto) {
        long userId = StpUtil.getLoginIdAsLong();
        LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(User::getId, StpUtil.getLoginIdAsLong());
        List<UserRoleRelation> roleRelationList = userRoleRelationService.list(new LambdaQueryWrapper<UserRoleRelation>()
                .eq(UserRoleRelation::getUserId, StpUtil.getLoginIdAsLong())
        );
        LambdaQueryWrapper<FabricApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getCertificateType()), FabricApply::getCertificateType, dto.getCertificateType())
                .like(StrUtil.isNotBlank(dto.getCorpName()), FabricApply::getCorpName, dto.getCorpName())
                .like(StrUtil.isNotBlank(dto.getApplyTransactionHash()), FabricApply::getApplyTransactionHash, dto.getApplyTransactionHash())
                .like(StrUtil.isNotBlank(dto.getFirstPersonName()), FabricApply::getFirstPersonName, dto.getFirstPersonName())
                .like(StrUtil.isNotBlank(dto.getFirstTransactionHash()), FabricApply::getFirstTransactionHash, dto.getFirstTransactionHash())
                .like(StrUtil.isNotBlank(dto.getFinalPersonName()), FabricApply::getFinalPersonName, dto.getFinalPersonName())
                .like(StrUtil.isNotBlank(dto.getFinalTransactionHash()), FabricApply::getFinalTransactionHash, dto.getFinalTransactionHash())
                .orderByDesc(FabricApply::getId)
                .select(FabricApply.class, x -> VoToColumnUtil.fieldsToColumns(FabricApplyPageVo.class).contains(x.getProperty()));
        if (roleRelationList != null && roleRelationList.size() > 0 &&
                RoleTypeEnum.masses.getRoleId().equals(roleRelationList.get(0).getRoleId())) {
            queryWrapper.eq(FabricApply::getCreateUserId, userId);
            if (StringUtils.isNotBlank(dto.getBusinessType()) && "ZJGL".equals(dto.getBusinessType())) {
                queryWrapper.eq(FabricApply::getCertificateStatus, "YFZ");
            }
        }
        if (roleRelationList != null && roleRelationList.size() > 0 &&
                RoleTypeEnum.dept.getRoleId().equals(roleRelationList.get(0).getRoleId())) {
            queryWrapper.ne(FabricApply::getApprovalStatus, "DSH")
                    .ne(FabricApply::getApprovalStatus, "CSSB");
        }
        if (roleRelationList != null && roleRelationList.size() > 0
                && RoleTypeEnum.dept.getRoleId().equals(roleRelationList.get(0).getRoleId())
                && StringUtils.isNotBlank(dto.getBusinessType()) && "ZJGL".equals(dto.getBusinessType())
        ) {
            queryWrapper.ne(FabricApply::getCertificateStatus, "DZZ");
        }
        IPage<FabricApply> page = fabricApplyService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<FabricApplyPageVo> pageOutput = ConventPage.getPageOutput(page, FabricApplyPageVo.class);
        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询FabricApply信息")
    @SaCheckPermission("fabricapply:detail")
    public R info(@RequestParam Long id) {
        FabricApply fabricApply = fabricApplyService.getById(id);
        if (fabricApply == null) {
            return R.error("找不到此数据！");
        }
        return R.ok(BeanUtil.toBean(fabricApply, FabricApplyVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增FabricApply")
    @SaCheckPermission("fabricapply:add")
    public R add(@Valid @RequestBody AddFabricApplyDto dto) {
        FabricApply fabricApply = BeanUtil.toBean(dto, FabricApply.class);
        return R.ok(fabricApplyService.add(fabricApply));
    }

    @PutMapping
    @ApiOperation(value = "修改FabricApply")
    @SaCheckPermission("fabricapply:edit")
    public R update(@Valid @RequestBody UpdateFabricApplyDto dto) {

        FabricApply fabricApply = BeanUtil.toBean(dto, FabricApply.class);
        return R.ok(fabricApplyService.edit(fabricApply));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("fabricapply:delete")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(fabricApplyService.delete(ids));
    }

    @PostMapping("firstApproval")
    @ApiOperation(value = "初审")
    @SaCheckPermission("fabricapply:firstApproval")
    public R firstApproval(@Valid @RequestBody UpdateFabricApplyDto dto) {
        FabricApply fabricApply = BeanUtil.toBean(dto, FabricApply.class);
        return R.ok(fabricApplyService.firstApproval(fabricApply));
    }

    @PostMapping("finalApproval")
    @ApiOperation(value = "终审")
    @SaCheckPermission("fabricapply:finalApproval")
    public R finalApproval(@Valid @RequestBody UpdateFabricApplyDto dto) {
        FabricApply fabricApply = BeanUtil.toBean(dto, FabricApply.class);
        return R.ok(fabricApplyService.finalApproval(fabricApply));
    }

    @PostMapping("transactCertificate")
    @ApiOperation(value = "办证")
    @SaCheckPermission("fabricapply:transactCertificate")
    public R transactCertificate(@Valid @RequestBody UpdateFabricApplyDto dto) {
        FabricApply fabricApply = BeanUtil.toBean(dto, FabricApply.class);
        return R.ok(fabricApplyService.transactCertificate(fabricApply));
    }

    @PostMapping("sendCertificate")
    @ApiOperation(value = "发证")
    @SaCheckPermission("fabricapply:sendCertificate")
    public R sendCertificate(@Valid @RequestBody UpdateFabricApplyDto dto) {
        FabricApply fabricApply = BeanUtil.toBean(dto, FabricApply.class);
        return R.ok(fabricApplyService.sendCertificate(fabricApply));
    }
}